package cli.command;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;

public class Remove implements Command {
    private Scanner in;
    private PrintWriter out;
    private Path currentPath;
    private boolean r = false; // 递归删除
    private boolean f = false; // 不询问

    public Remove(Scanner in, PrintWriter out, Path currentPath) {
        this.in = in;
        this.out = out;
        this.currentPath = currentPath;
    }

    @Override
    public void handle(String[] args) {
        int pos = 0;
        if (pos < args.length && args[pos].startsWith("-")) {
            setOptions(args[pos]);
            pos++;
        }
        if (pos >= args.length || args[pos].isEmpty()) {
            out.println("请输入路径！");
            return;
        }
        Path path = currentPath.resolve(args[pos]);
        if (r) {
            removeRecur(path);
        } else {
            remove(path);
        }
    }

    private void setOptions(String options) {
        r = options.contains("r");
        f = options.contains("f");
    }

    private void remove(Path path) {
        try {
            Files.delete(path);
        } catch (NoSuchFileException e) {
            out.println("文件不存在！");
        } catch (DirectoryNotEmptyException e) {
            out.println("文件夹非空！");
        } catch (IOException e) {
            out.println("删除失败！");
        }
    }

    private void removeRecur(Path path) {
        try {
            Files.walkFileTree(path, new FileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    boolean delete = true;
                    if (!f) {
                        out.println("是否删除文件夹"+currentPath.relativize(dir)+"？");
                        delete = in.nextLine().matches(".*是|y|Y.*");
                    }
                    return delete ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    boolean delete = true;
                    if (!f) {
                        out.println("是否删除文件"+currentPath.relativize(file)+"？");
                        delete = in.nextLine().contains("是");
                    }
                    if (delete) {
                        try {
                            Files.delete(file);
                        } catch (IOException e) {
                            out.println("删除文件"+currentPath.relativize(file)+"失败！");
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    out.println("无法访问文件"+currentPath.relativize(file)+"!");
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    try {
                        if (Files.list(dir).count() == 0) {
                            Files.delete(dir);
                        }
                    } catch (IOException e) {
                        out.println("删除文件夹"+currentPath.relativize(dir)+"失败！");
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (FileNotFoundException e) {
            out.println("文件不存在！");
        } catch (IOException e) {
            out.println("删除失败！");
        }
    }
}
